package com.lin.eat.controller;

import com.github.pagehelper.PageInfo;
import com.lin.eat.entry.*;
import com.lin.eat.serivce.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取推荐商品列表
     * @return
     */
    @GetMapping("/recommendGoods")
    public ServerResponse<List<Goods>> recommendGoods(){
        return goodsService.recommendGoods();
    }

    /**
     * 获取热销商品列表
     * @return
     */
    @GetMapping("/hotGoods")
    public ServerResponse<Map<String,List<Goods>>> hotGoods(){
        return goodsService.hotGoods();
    }

    /**
     * 获取全部商品
     * @param pageParam
     * @return
     */
    @PostMapping("/getAllGoods")
    public ServerResponse<PageInfo<Goods>> getAllGoods(@RequestBody PageParam pageParam) {
        return goodsService.selectAllGoods(pageParam.getPageNum(), pageParam.getPageSize());
    }

    /**
     * 获取商品详情
     * @param g_id
     * @return
     */
    @GetMapping("/getGoodsDetail/{g_id}")
    public ServerResponse<Map<String,GoodsDetail>> getGoodsDetail(@PathVariable("g_id") String g_id) {
        return goodsService.getGoodsDetail(g_id);
    }

    /**
     * 通过关键字查找商品
     * @param pageParam
     * @return
     */
    @PostMapping("/getGoodsByKey")
    public ServerResponse<PageInfo<Goods>> getGoodsByKey(@RequestBody PageParam pageParam) {
        return goodsService.getGoodsByKey(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getKey());
    }

    @PostMapping("/addGoods")
    public String addGoods(MultipartFile file, Good good) throws IOException {
        goodsService.addGoods(file,good);
        return "yes";
    }

}


